<?php
namespace app\api\controller\contract;
use think\File;
use think\facade\Env;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use app\common\model\company\Contract;
use app\common\model\orderdata\Goods;
use app\common\model\quotation\QuotationList;
use app\common\model\orderhawa\HawaList;
use app\common\model\orderhawa\Hawa;
use app\api\controller\Base;
use app\api\controller\Pub;
use app\common\model\power\Rolepower;
use app\api\controller\mytool\Sendmessage;

class Contracthawa extends Base
{
	/**
	 * 初始化
	 */
    public function initialize(){
		parent::initialize();
		self::is_login();
		Rolepower::powerguard(101003);
	}
	
	/**
	 * 获取选择的收集单位信息
	 * @return [type] [description]
	 */
	public function getinfo(){
		$id = input('id');
		if(empty($id)){
			return myerr('没有找到合同信息');
		}
		$data = Contract::getinfo($id,'hawa');
		if(!$data['code']){
			return myerr($data['msg']);
		}
		$data = $data['msg'];
		$sdata['Type'] = 'contract';
		$sdata['where'] = [['CompanyId',$data['CompanyId']],['ContractId','<>',$id]];
		$data['contractlist'] = Contract::usergetlist($sdata,'buss',false);
		$data['buss'] = UserModel::getpublicinfo($data['BussinessId']);
		$data['StateList'] = Contract::STATEARRAY;
		return myok($data);
	}

	/**
	 * 获取订单列表
	 * @return [type] [description]
	 */
	public function list(){
		$data = input('get.');
		$data['Type'] = 'contract';
		$data = init_where($data);
		$list = Contract::usergetlist($data,'hawa');
		
		foreach ($list['list'] as $key => $value) {
			if(empty($value['bussinfo']['Name'])){
				$list['list'][$key]['bussinfoName'] = '暂未完善名称';
			}else{
				$list['list'][$key]['bussinfoName'] = $value['bussinfo']['Name'];
			}
			
		}
		$list['tr']['title']  = ['签约人','下单时间','订单状态','查看','生效时间','结束时间'];
		$list['tr']['key'] = ['bussinfoName','CreateTime','State','ContractId','StartTime','EndTime'];
		$list['tr']['show']=[true,true,true,true,false,false];
		return myok($list);
	}
	/**
	 * 接单
	 * @return [type] [description]
	 */
	public function confrim(){
		$id = input('id');
		$data = Contract::liststate($id);
		if(empty($data)){
			return myerr('没有找到需要的合同信息');
		}
		$data = $data['msg'];
		
		$companyinfo = Company::user_getinfo($data['CompanyId']);
		if($companyinfo['UserId']!==Pub::userid()){
			return myerr('非公司负责人，没有权限');
		}
		$rdata = [];
		$rdata['id'] = $id;
		$rdata['State'] = Contract::STATECONFRIM;
		Contract::user_reinfo($rdata);
		Sendmessage::Contract(2,$id,$data);
		return myok('合同信息确认成功');
	}

	/**
	 * 图片上传
	 * @return [type] [description]
	 */
	public function upimg(){
		$type = empty(input('type'))?'other':input('type');
		if(!$type=='hawa'||!$type=='buss'||!$type=='acco'){
			$type = 'other';
		}
		try {
			$file = request()->file('img');
		} catch (\Exception $e) {
			return json_encode(['status'=>'error','data'=>$e->getMessage()]);
		}
        // 移动到框架应用根目录/public/uploads/ 目录下
        if(!is_null($file)){
           	try{
           		$save_path = Env::get('root_path').'public\\static' .'\\upimg\\countract\\'.$type.'\\';
	            $info = $file->validate(["size"=>50000000,'ext'=>'jpg,png,gif'])->rule("date")->move($save_path);
	            //echo $save_path. DS . date("Ymd") . DS .  $info->getFilename();
	            $image = \think\Image::open($save_path . '\\' . $info->getSaveName());  
	            $image->thumb(2000, 2000)->save($save_path .  '\\' . $info->getSaveName());  
	            if($info){//getSaveName();
	                $p=$info->getFilename();
	                return json_encode(['status'=>'ok','data'=>SERVERURL.'static/upimg/countract/'.$type.'/'.date('Ymd',time()).'/'.$p]);
	            }else{
	              return json_encode(['status'=>'error','data'=>$info->getError()]);
	            }
           	} catch (\Exception $e) {
				return json_encode(['status'=>'error','data'=>$e->getMessage()]);
			}	
            
         }else{
              return json_encode(['status'=>'error','data'=>'没有找到需要上传的图片']);
         }
	}


	/**
	 * 判断账号是否登录
	 * @return boolean [description]
	 */
	private function is_login(){
		$data = UserModel::is_sessionlogin();
		if (!$data['code']) {
			$data['options'] = 'relogin';
			json($data)->send();
			exit;
		}
	}

	/**
	 * 获取合同列表
	 * @param Contract $contract
	 * @return \think\response\Json
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function selectContract(Contract $contract)
	{
		$lists = $contract->contractLists(1);
		return myok($lists);
	}

}
